<template>
  <div class="mine-container">
    <!-- 用户信息区 -->
    <div class="user-info-card">
      <div class="avatar">
        <img :src="userInfo.avatar || defaultAvatar" alt="用户头像">
      </div>
      <div class="user-details">
        <h2 class="user-name">{{ userInfo.name || '未登录' }}</h2>
        <p class="user-role">教师</p>
      </div>
    </div>
    
    <!-- 资源库区域 -->
    <div class="section">
      <div class="section-header">
        <h3 class="section-title">我的资源库</h3>
        <div class="filter-tabs">
          <span 
            v-for="tab in filterTabs" 
            :key="tab.value"
            :class="['filter-tab', { active: currentFilter === tab.value }]"
            @click="setFilter(tab.value)"
          >
            {{ tab.label }}
          </span>
        </div>
      </div>
      
      <div class="resource-list" v-if="filteredResources.length > 0">
        <div 
          v-for="resource in filteredResources" 
          :key="resource.id"
          class="resource-card"
          @click="viewResourceDetail(resource.id)"
        >
          <div class="resource-icon" :class="resource.type">
            <img :src="getResourceIcon(resource.type)" :alt="resource.type">
          </div>
          <div class="resource-content">
            <h4 class="resource-title">{{ resource.title }}</h4>
            <p class="resource-time">{{ resource.createdAt }}</p>
          </div>
        </div>
      </div>
      
      <div class="empty-state" v-else>
        <img src="/src/assets/icons/empty.svg" alt="暂无资源">
        <p>暂无资源，快去创建吧！</p>
      </div>
    </div>
    
    <!-- 用户画像数据 -->
    <div class="section">
      <h3 class="section-title">用户画像</h3>
      
      <div class="profile-card">
        <div class="profile-item">
          <h4>课程选择</h4>
          <div class="tags">
            <span v-for="(course, index) in userProfile.courses" :key="index" class="tag">
              {{ course }}
            </span>
          </div>
        </div>
        
        <div class="profile-item">
          <h4>风格偏好</h4>
          <div class="tags">
            <span v-for="(style, index) in userProfile.styles" :key="index" class="tag">
              {{ style }}
            </span>
          </div>
        </div>
        
        <div class="profile-item">
          <h4>反馈记录</h4>
          <div class="feedback-stats">
            <div class="stat-item">
              <span class="stat-value">{{ userProfile.feedbackCount }}</span>
              <span class="stat-label">总反馈数</span>
            </div>
            <div class="stat-item">
              <span class="stat-value">{{ userProfile.satisfactionRate }}%</span>
              <span class="stat-label">满意度</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../../stores/userStore'
import apiService from '../../api'

const router = useRouter()
const userStore = useUserStore()

// 默认头像
const defaultAvatar = '/src/assets/icons/default-avatar.svg'

// 用户信息
const userInfo = computed(() => userStore.userInfo)

// 筛选标签
const filterTabs = [
  { label: '全部', value: 'all' },
  { label: '教学设计', value: 'design' },
  { label: '多媒体', value: 'media' },
  { label: '练习题', value: 'exercise' },
  { label: '试卷', value: 'paper' }
]

// 当前筛选类型
const currentFilter = ref('all')

// 资源列表
const resourceList = ref([
  {
    id: '1',
    title: '《人工智能概述》教学设计',
    type: 'design',
    createdAt: '2023-08-07 10:30'
  },
  {
    id: '2',
    title: '机器学习算法图解',
    type: 'media',
    createdAt: '2023-08-06 15:45'
  },
  {
    id: '3',
    title: '深度学习基础练习题',
    type: 'exercise',
    createdAt: '2023-08-05 09:20'
  },
  {
    id: '4',
    title: '人工智能期中考试试卷',
    type: 'paper',
    createdAt: '2023-08-04 14:10'
  }
])

// 用户画像数据
const userProfile = ref({
  courses: ['人工智能', '机器学习', '深度学习', '数据结构'],
  styles: ['案例驱动', '互动式教学', '理论精讲'],
  feedbackCount: 15,
  satisfactionRate: 92
})

// 根据筛选条件过滤资源列表
const filteredResources = computed(() => {
  if (currentFilter.value === 'all') {
    return resourceList.value
  } else {
    return resourceList.value.filter(item => item.type === currentFilter.value)
  }
})

// 设置筛选条件
const setFilter = (filter) => {
  currentFilter.value = filter
}

// 获取资源图标
const getResourceIcon = (type) => {
  const iconMap = {
    design: '/src/assets/icons/design.svg',
    media: '/src/assets/icons/media.svg',
    exercise: '/src/assets/icons/exercise.svg',
    paper: '/src/assets/icons/paper.svg'
  }
  return iconMap[type] || '/src/assets/icons/default.svg'
}

// 查看资源详情
const viewResourceDetail = (id) => {
  router.push(`/resource/detail?id=${id}`)
}

// 页面加载时获取数据
onMounted(async () => {
  // 初始化用户数据
  userStore.initUserData()
  
  try {
    // 获取资源列表
    // 实际项目中应该调用API获取资源列表
    // const response = await apiService.resource.getList()
    // if (response && response.data) {
    //   resourceList.value = response.data
    // }
  } catch (error) {
    console.error('获取资源列表失败:', error)
  }
})
</script>

<style scoped>
.mine-container {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

/* 用户信息卡片样式 */
.user-info-card {
  display: flex;
  align-items: center;
  background-color: white;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.avatar {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 16px;
  background-color: #f0f5ff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-name {
  margin: 0 0 5px 0;
  font-size: 1.3rem;
  color: #333;
}

.user-role {
  margin: 0;
  font-size: 0.9rem;
  color: #666;
}

/* 区块通用样式 */
.section {
  background-color: white;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.section-title {
  font-size: 1.2rem;
  margin: 0;
  color: #333;
  font-weight: 600;
}

/* 筛选标签样式 */
.filter-tabs {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.filter-tab {
  font-size: 0.9rem;
  padding: 6px 12px;
  border-radius: 16px;
  background-color: #f5f5f5;
  color: #666;
  cursor: pointer;
  transition: all 0.2s;
}

.filter-tab.active {
  background-color: #4b6cb7;
  color: white;
}

/* 资源列表样式 */
.resource-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

.resource-card {
  background-color: #f9f9f9;
  border-radius: 10px;
  padding: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.resource-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.resource-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
}

.resource-icon.design {
  background-color: #e6f7ff;
}

.resource-icon.media {
  background-color: #f6ffed;
}

.resource-icon.exercise {
  background-color: #fff7e6;
}

.resource-icon.paper {
  background-color: #f9f0ff;
}

.resource-icon img {
  width: 24px;
  height: 24px;
}

.resource-title {
  margin: 0 0 8px 0;
  font-size: 1rem;
  color: #333;
  font-weight: 500;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.resource-time {
  margin: 0;
  font-size: 0.8rem;
  color: #999;
}

/* 空状态样式 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
  color: #999;
}

.empty-state img {
  width: 80px;
  height: 80px;
  margin-bottom: 16px;
  opacity: 0.5;
}

/* 用户画像样式 */
.profile-card {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.profile-item h4 {
  margin: 0 0 10px 0;
  font-size: 1rem;
  color: #333;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag {
  font-size: 0.9rem;
  padding: 4px 10px;
  border-radius: 16px;
  background-color: #f0f5ff;
  color: #4b6cb7;
}

.feedback-stats {
  display: flex;
  gap: 20px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 600;
  color: #4b6cb7;
}

.stat-label {
  font-size: 0.9rem;
  color: #666;
  margin-top: 4px;
}

/* 响应式调整 */
@media (max-width: 600px) {
  .section-header {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .filter-tabs {
    margin-top: 12px;
    width: 100%;
    overflow-x: auto;
    padding-bottom: 8px;
  }
  
  .resource-list {
    grid-template-columns: 1fr;
  }
  
  .feedback-stats {
    justify-content: space-around;
    width: 100%;
  }
}
</style>